import json
from PySide2.QtWidgets import QWidget
from PySide2.QtCore import Qt,QPoint,Signal,QMimeData
from PySide2.QtGui import QDrag,QMouseEvent
from widget.ui_dataInfo import Ui_Form_dataInfo
from widget.clickPosi import ClickPosi

class DataInfoWidget(QWidget):

    X = 'x'
    Y = 'y'
    INTERVAL = 'interval'
    EN = 'able'
    MARKE = 'marke'
    
    SigMessage = Signal(str)
    SigFindPosi = Signal(bool)
    SigSendSelf = Signal(object)

    def __init__(self, parent: QWidget = None, flags: Qt.WindowFlags = Qt.WindowFlags()) -> None:
        super().__init__(parent, flags)

        self.ui = Ui_Form_dataInfo()
        self.ui.setupUi(self)
        self.setAcceptDrops(True)
        self.ui.pushButton_getXY.clicked.connect(self.but_clicked_posi)
        self.ui.pushButton_del.clicked.connect(lambda:self.SigSendSelf.emit(self))

    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        
        if int(event.buttons()& Qt.MouseButton.LeftButton):
            md = QMimeData()
            md.setText(json.dumps(self.datainfo(),indent=4))
            qd = QDrag(self)
            qd.setMimeData(md)
            qd.setHotSpot(event.pos())
            qd.setPixmap(self.grab())
            if qd.exec_(Qt.DropAction.CopyAction|Qt.DropAction.MoveAction, Qt.DropAction.CopyAction) == Qt.DropAction.MoveAction:
                self.SigSendSelf.emit(self)
            event.accept()

    def update_posixy(self, p:QPoint):

        self.ui.spinBox_x.setValue(p.x())
        self.ui.spinBox_y.setValue(p.y())
        self.SigMessage.emit(f'获取到的坐标位置为 X={p.x()} Y={p.y()}')

    def but_clicked_posi(self):

        self.SigFindPosi.emit(True)
        w = ClickPosi()
        w.SigPosi.connect(self.update_posixy)
        w.exec_()
        self.SigFindPosi.emit(False)
        w.deleteLater()
    
    def datainfo(self) -> dict:

        d = {}
        d.setdefault(DataInfoWidget.X, self.ui.spinBox_x.value())
        d.setdefault(DataInfoWidget.Y, self.ui.spinBox_y.value())
        d.setdefault(DataInfoWidget.INTERVAL, self.ui.doubleSpinBox_interval.value())
        d.setdefault(DataInfoWidget.EN, self.ui.checkBox_able.isChecked())
        d.setdefault(DataInfoWidget.MARKE, self.ui.lineEdit_marke.text())
        return d
    
    def __str__(self) -> str:

        return f'X={self.ui.spinBox_x.value()}\tY={self.ui.spinBox_y.value()}\t{self.ui.lineEdit_marke.text()}'
    
    @staticmethod
    def from_data_create_widget(d:dict, parent:QWidget=None) -> QWidget:

        w = DataInfoWidget(parent)
        try:
            w.ui.spinBox_x.setValue(d.get(DataInfoWidget.X, 0))
        except:
            pass
        try:
            w.ui.spinBox_y.setValue(d.get(DataInfoWidget.Y, 0))
        except:
            pass
        try:
            w.ui.doubleSpinBox_interval.setValue(d.get(DataInfoWidget.INTERVAL, 0))
        except:
            pass
        try:
            w.ui.checkBox_able.setChecked(d.get(DataInfoWidget.EN, False))
        except:
            pass
        try:
            w.ui.lineEdit_marke.setText(d.get(DataInfoWidget.MARKE, False))
        except:
            pass
        return w